package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.model.Limits;
import com.prince.model.User;
import com.prince.web.base.BaseController;
import com.prince.web.service.LimitsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * 权限
 * @author Wangpeixi 
 *
 */
@Controller
public class LimitController extends BaseController{

	@Resource
	private LimitsService limitService;
	
	/**
	 * 通过用户id 查询菜单列表
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@AppLog
	@RequestMapping(value = "findLimitsByUserId", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findLimitsByUserId(HttpServletRequest request,HttpServletResponse response) throws IOException {
		//通过session 获取用户id
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null) {
			response.sendRedirect("<script>window.top.location.href =\\\"/login.html\\\"</script>");
			return null;
		}
		
		Map<String,Object> map = limitService.findLimitsByUserId(user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 查询limits
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findLimitsAll", method = RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findLimitsAll(@RequestParam Map<String,Object> params,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		Map<String,Object> map = limitService.findLimitsAll(user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 根据parentCode查询
	 * @param limits
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findLimitsByParentCode",method =RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findLimitsByParentCode(Limits limits){
		Map<String,Object> map = limitService.findLimitsByParentCode(limits);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 添加权限
	 * @param limits
	 * @return
	 */
	@AppLog
	@RequestMapping(value="addLimits",method =RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> addLimits(Limits limits,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		Map<String,Object> map = limitService.addLimits(limits,user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 添加权限
	 * @param limits
	 * @return
	 */
	@AppLog
	@RequestMapping(value="delLimits",method =RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> delLimits(Limits limits,HttpServletRequest request){
		Map<String,Object> map = limitService.delLimits(limits);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 根据id查询权限
	 * @param limits
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findLimitsById",method =RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findLimitsById(Limits limits){
		Map<String,Object> map = limitService.findLimitsById(limits.getId());
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 更新权限
	 * @param limits
	 * @return
	 */
	@AppLog
	@RequestMapping(value="updateLimits",method =RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> updateLimits(Limits limits){
		Map<String,Object> map = limitService.updateLimits(limits);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	/**
	 * 通过userId、parentCode 查询按钮权限
	 * @param params
	 * @return
	 */
	@AppLog
	@RequestMapping(value="selectLimitsByUserIdAndParentCode",method =RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> selectLimitsByUserIdAndParentCode(@RequestParam Map<String,Object> params,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		Map<String,Object> map = limitService.selectLimitsByUserIdAndParentCode(params,user);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}

	/**
	 *
	 * @param limits
	 * @return
	 */
	@AppLog
	@RequestMapping(value="findCodeByAddr",method =RequestMethod.POST)
	public ResponseEntity<Map<String,Object>> findCodeByAddr(Limits limits){
		Map<String,Object> map = limitService.findCodeByAddr(limits);
		return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
	}
	
	
}
